<template>
  <div>
    <Card>
      <p slot="title">
        管理员
      </p>
      <div slot="extra">
        <Button class="icon-btn" type="success" @click="alertAdd">
          <span class="ml4">添加</span></Button>
      </div>
      <Table ref="tables" :data="tableData" :columns="columns" border disabled-hover/>
      <div class="page-warp">
        <Page
            :current="page.current"
            :page-size="page.size"
            :total="page.total"
            show-total
            @on-change="pageChange"></Page>
      </div>
    </Card>

    <Modal v-model="modalSetting.show" width="668" :styles="{top: '20%'}" @on-visible-change="doCancel" :mask-closable="false">
      <p slot="header" style="color:#2d8cf0;">
        <Icon type="md-information-circle"></Icon>
        <span>{{formItem.id ? '编辑' : '新增'}}用户</span>
      </p>
      <Form ref="myForm" :rules="ruleValidate" :model="formItem" :label-width="80">
        <FormItem label="真实姓名" prop="name">
          <Input v-model="formItem.name" placeholder="请输入账号"></Input>
        </FormItem>
        <FormItem label="用户账号" prop="username">
          <Input v-model="formItem.username" placeholder="请输入账号"></Input>
        </FormItem>
        <FormItem label="用户密码" prop="password">
          <Input v-model="formItem.password" type="password" placeholder="用户密码"></Input>
        </FormItem>
        <FormItem label="权限组" prop="group_id">
          <CheckboxGroup v-model="formItem.group_id">
            <Checkbox v-for="group in groupList" :key="group.id" :label="group.id + ''">{{ group.name }}</Checkbox>
          </CheckboxGroup>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancel" style="margin-right: 8px">取消</Button>
        <Button type="primary" @click="submit" :loading="modalSetting.loading">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>

import Icons from '_c/icons'
import { getUsers, addUser, editUser, delUser, getGroups } from '@/api/system'
export default {
  name: 'user',
  components: {
    Icons
  },
  data () {
    return {
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      columns: [
        { title: '序号', key: 'id', width: 65, align: 'center' },
        { title: '名称', key: 'name', align: 'center' },
        { title: '用户名', key: 'username', align: 'center' },
        {
          title: '创建时间',
          key: 'created_at',
          width: 150,
          align: 'center'
        },
        {
          title: '状态',
          key: 'status',
          width: 100,
          align: 'center',
          render: (h, params) => {
            let _this = this
            let { id, status } = params.row
            return h('i-switch', {
              attrs: {
                size: 'large'
              },
              props: {
                'true-value': 1,
                'false-value': 0,
                value: status
              },
              on: {
                'on-change': (status) => {
                  editUser(id, { status }).then(res => {
                    if (res.code === 1) {
                      _this.$Message.success(res.msg)
                    } else {
                      _this.$Message.error(res.msg)
                    }
                  })
                }
              }
            }, [
              h('span', {
                slot: 'open'
              }, '启用'),
              h('span', {
                slot: 'close'
              }, '禁用')
            ])
          }
        },
        {
          title: '操作',
          key: 'action',
          width: 200,
          align: 'center',
          render: (h, params) => {
            let _this = this
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click () {
                    let { id,name, username, group_id } = params.row
                    _this.formItem.id = id
                    _this.formItem.name = name
                    _this.formItem.username = username
                    _this.formItem.password = 'YCMS'
                    getGroups().then(function (res) {
                      if (res.code === 1) {
                        _this.groupList = res.data.list
                      } else {
                        _this.$Message.error(res.msg)
                      }
                    })
                    _this.formItem.group_id = group_id
                    _this.modalSetting.show = true
                    _this.modalSetting.index = params.index
                  }
                }
              }, '编辑'),
              h('Poptip', {
                style: {
                  'textAlign': 'left'
                },
                props: {
                  confirm: true,
                  title: '确定删除？'
                },
                on: {
                  'on-ok': () => {
                    let { id } = params.row
                    delUser(id).then(res => {
                      if (res.code === 1) {
                        this.$Message.success(res.msg)
                        this.getList()
                      } else {
                        this.$Message.error(res.msg)
                      }
                    })
                  }
                }
              }, [
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  }
                }, '删除')
              ])
            ])
          }
        }
      ],
      tableData: [],
      groupList: [],
      modalSetting: {
        show: false,
        loading: false,
        index: 0
      },
      formItem: {
        name: '',
        username: '',
        password: '',
        group_id: [],
        id: 0
      },
      ruleValidate: {
        name: [
          { required: true, message: '用户昵称不能为空', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '用户密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit () {
      let _this = this
      this.$refs['myForm'].validate((valid) => {
        if (valid) {
          _this.modalSetting.loading = true
          if(_this.formItem.id){
            editUser(_this.formItem.id, _this.formItem).then(function (res) {
              if (res.code === 1) {
                _this.$Message.success(res.msg)
                _this.getList()
              } else {
                _this.$Message.error(res.msg)
              }
              _this.cancel()
            })
          }else{
            addUser(_this.formItem).then(function (res) {
              if (res.code === 1) {
                _this.$Message.success(res.msg)
                _this.getList()
              } else {
                _this.$Message.error(res.msg)
              }
              _this.cancel()
            })
          }
        }
      })
    },
    cancel () {
      this.modalSetting.show = false
    },
    doCancel (data) {
      if (!data) {
        this.formItem.id = 0
        this.$refs['myForm'].resetFields()
        this.modalSetting.loading = false
        this.modalSetting.index = 0
      }
    },
    alertAdd () {
      let _this = this
      getGroups().then(function (res) {
        if (res.code === 1) {
          _this.groupList = res.data.list
        } else {
          _this.$Message.error(res.msg)
        }
      })
      this.modalSetting.show = true
    },
    remove (index) {
      this.tableData.splice(index, 1)
      if (index === 0 && (this.page.current > 1)) {
        this.page.current = this.page.current - 1
        this.getList()
      } else {
        this.page.total = this.page.total - 1
      }
    },
    pageChange (page) {
      this.page.current = page
      this.getList()
    },
    getList () {
      let { current: page, size } = this.page
      getUsers({ page, size }).then(res => {
        if (res.code === 1) {
          this.tableData = res.data.list
          this.page.total = res.data.count
        }
      })
    }
  },
  mounted () {
    this.getList()
  }
}
</script>

<style>
.ivu-card-head p{
  height: 32px;
  line-height: 32px;
}
</style>
